# routers/messages.py

from fastapi import APIRouter, Depends

from core.crud import create_message, get_messages
from core.dependencies import common_dependencies
from core.schemas import MessageCreate, ResponseModel

router = APIRouter(
    prefix="/messages",
    tags=["messages"],
    responses={404: {"description": "Not found"}},
)


@router.post("/create", response_model=ResponseModel)
async def create_message_endpoint(message: MessageCreate, common: dict = Depends(common_dependencies)):
    db_message = create_message(common["db"], message.conversation_id, common["user_id"], message.message_content, message.sender)
    return {"data": db_message, "code": "success", "message": "Message created successfully"}


@router.get("/conversation/{conversation_id}", response_model=ResponseModel)
async def get_messages_endpoint(conversation_id: str, common: dict = Depends(common_dependencies)):
    messages = get_messages(common["db"], conversation_id=conversation_id)
    if not messages:
        return {"data": [], "code": "success", "message": "No messages found for this conversation"}
    return {"data": messages, "code": "success", "message": "Messages retrieved successfully"}
